<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/Public/foundation/foundation.min.css">
    <link rel="stylesheet" href="/Public/foundation/jquery.datetimepicker.css">
    <script src="/Public/jquery/jquery.min.js"></script>
    <script src="/Public/foundation/foundation.min.js"></script>
    <script src="/Public/foundation/modernizr.js"></script>
    <script src="/Public/jquery/plugin/cityselect/jquery.cityselect.js"></script>

    <!--<script src="/Public/jquery/plugin"></script>-->
    <link rel="stylesheet" href="http://static.runoob.com/assets/foundation-icons/foundation-icons.css">
</head>
<body>
<nav class="top-bar" data-topbar>
    <ul class="title-area">
        <li class="name">
            <h1><a href="#">后台管理</a></h1>
        </li>
    </ul>
    <section class="top-bar-section">
        <ul class="right">
                <li><a href="">欢迎<?php echo (session('name')); ?></a></li>
                <li><a href="<?php echo U('./Sys/Login/doLogout');?>">退出</a></li>
        </ul>

    </section>
</nav>
<div>
    <div class="small-2 columns">
        <?php if(is_array($locks)): $i = 0; $__LIST__ = $locks;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$menu): $mod = ($i % 2 );++$i;?><ul class="side-nav">
                <li><a href="http://127.0.0.1/index.php/<?php echo ($menu["url"]); ?>" target="<?php echo ($menu['target']==0?'_self':'_blank'); ?>"><?php echo ($menu["mname"]); ?></a></li>
            </ul><?php endforeach; endif; else: echo "" ;endif; ?>
    </div>
    <div class="small-10 columns">
        
    <table  id="menusTbl">
        <thead>
        <tr>
            <th><input  onclick="selectAll(this)" type="checkbox"></th>
            <th>编号</th>
            <th>菜单名称</th>
            <th>菜单路径</th>
            <th>上级菜单</th>
            <th>分组</th>
            <th>排序</th>
            <th>打开方式</th>
            <th>锁定</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php if(is_array($navs)): $i = 0; $__LIST__ = $navs;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$nav): $mod = ($i % 2 );++$i;?><tr id="row_<?php echo ($nav["menu_id"]); ?>">
            <td><input class="slct" type="checkbox" value="<?php echo ($nav["menu_id"]); ?>"></td>
            <td><?php echo ($nav["menu_id"]); ?></td>
            <td><input name="mname" class="a" disabled type="text" value="<?php echo ($nav["mname"]); ?>"></td>
            <td><input name="url" class="a" disabled type="text" value="<?php echo ($nav["url"]); ?>"></td>
            <!--<td><input name="up" class="a" disabled style="width:4em;" type="text" value="<?php echo ($nav["up"]); ?>"></td>-->
             <td>
                 <select  class="a" name="up" id="" disabled>
                     <option <?php echo ($nav['up']==0?'selected':null); ?> value="0" >顶级菜单</option>
                     <?php if(is_array($opt)): $i = 0; $__LIST__ = $opt;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$v): $mod = ($i % 2 );++$i;?><option <?php echo ($nav['up']==$v['menu_id']?'selected':null); ?>
                         value="<?php echo ($v['menu_id']); ?>"><?php echo ($v["mname"]); ?> </option><?php endforeach; endif; else: echo "" ;endif; ?>
                 </select>
             </td>
            <td><input name="mgroup" class="a" disabled style="width:4em;" type="text" value="<?php echo ($nav["mgroup"]); ?>"></td>
            <td><input name="msort" class="a" disabled style="width:4em;" type="text" value="<?php echo ($nav["msort"]); ?>"></td>
            <td>
                <div class="switch tiny">
                    <input onclick="target(this)" value="<?php echo ($nav["menu_id"]); ?>" id="target_<?php echo ($nav["menu_id"]); ?>" type="checkbox"  <?php echo ($nav['target']?'checked':''); ?>>
                    <label for="target_<?php echo ($nav["menu_id"]); ?>"></label>
                </div>
            </td>

            <td>
                <div class="switch tiny">
                    <input onclick="lock(this)" value="<?php echo ($nav["menu_id"]); ?>" id="lock_<?php echo ($nav["menu_id"]); ?>" type="checkbox" <?php echo ($nav['lock']?'checked':''); ?> >
                    <label for="lock_<?php echo ($nav["menu_id"]); ?>"></label>
                </div>
            </td>
            <td>
                <button onclick="edit('row_<?php echo ($nav["menu_id"]); ?>')" class="button tiny edit">编辑</button>
                <button onclick="save('<?php echo ($nav["menu_id"]); ?>')" style="display:none;" class="button tiny save">保存</button>
            </td>

        </tr><?php endforeach; endif; else: echo "" ;endif; ?>
        </tbody>
    </table>
    <div>
        <span><?php echo ($page); ?></span>
    </div>
    <a class="button tiny" href="<?php echo U('addMenu');?>">添加</a>
    <button onclick="unSelect()" class="button tiny">反选</button>
    <button onclick="delLot()" class="button tiny">删除(批量)</button>
    <script>
        function edit(rowid) {
            $('#'+rowid+' .a').prop('disabled',false);
            $('#'+rowid+' .edit').hide();
            $('#'+rowid+' .save').show();
            //console.log('#'+id);exit;
        }
        function save(id) {
            $('#row_'+id+' .a').prop('disabled',true);
            $('#row_'+id+' .edit').show();
            $('#row_'+id+' .save').hide();

            var data = {
                menu_id:id,
                mname:$('#row_'+id+' [name=mname]').val(),
                url:$('#row_'+id+' [name=url]').val(),
                mgroup:$('#row_'+id+' [name=mgroup]').val(),
                msort:$('#row_'+id+' [name=msort]').val(),
                up:$('#row_'+id+' [name=up]').val(),
            }
            console.log(data);

            $.post('<?php echo U("doEditMenu");?>',data,function (data) {
                if(data.error!==0){
                    alert(data.msg);
                }
            });

        }
        //全选
        function selectAll(obj) {
            $('#menusTbl tbody .slct').prop('checked',$(obj).prop('checked'));
        }
        //反选
        function unSelect() {
            $('#menusTbl tbody .slct').map(function(){
                $(this).prop('checked',!$(this).prop('checked'));
            });
        }

        //批量删除
        function delLot() {//you
            if(!confirm('确认批量删除吗')){
                return;
            }
            var ids = $('#menusTbl tbody .slct:checked').map(function () {
                $(this).parent().parent().remove();
                return $(this).val();
            });
            ids  = ids.get().join();
            $.post('<?php echo U("doDelMenu");?>',{ids:ids},function (data) {
                alert(data.msg);
            });
        }

        //锁定
        function lock(obj) {
            var id = $(obj).val();
            var lockval = $(obj).prop('checked')?1:0;
            $.post('<?php echo U("doEditMenu");?>',{menu_id:id,lock:lockval});
        }

        //打开方式
        function target(obj) {
            var id = $(obj).val();
            var targetval = $(obj).prop('checked')?1:0;
            $.post('<?php echo U("doEditMenu");?>',{menu_id:id,target:targetval},function (data) {
                if(data.error!==0){
                    alert(data.msg);
                }
            });
        }

    </script>


    </div>
</div>
</body>
</html>